<template>
  <div class="page">
    <div class="bg-white pd">
      <div class="fw fs-h mgb">{{name}}</div>
      <div style="border-bottom: 1px dashed #ddd;" class="mgb"></div>
      <blocks :value="dataBlocks"></blocks>
    </div>
    <div class="fix-bottom">
      <div class="weui-flex v-mdl">
        <div class="weui-flex__item">
          <notify-btn cls="ui btn secondary" :loading="isLoading" @hit="onSave">
            仅保存
          </notify-btn>
        </div>
        <div class="weui-flex__item">
          <notify-btn cls="ui btn primary no-rounder" @hit="onPublish">
            去布置
          </notify-btn>
      </div>
      </div>
    </div>
    <page-common></page-common>
  </div>
</template>

<script>
import PageCommon from '@/components/page-common'
import {mapGetters, mapActions} from 'vuex'
import Blocks from '@/components/blocks'
import NotifyBtn from '@/components/notify-btn'
import base from '@/mixins/base'

export default {
  mixins: [base],
  components: {
    PageCommon,
    Blocks,
    NotifyBtn
  },
  computed: {
    ...mapGetters([
      'blocks',
      'loginer'
    ]),
    ...mapGetters('document', [
      'document',
      'name',
      'contentBlock',
      'operation'
    ]),
    dataBlocks () {
      return [this.contentBlock, ...this.blocks]
    }
  },
  methods: {
    ...mapActions('document', [
      'onCreate'
    ]),
    async onSave () {
      try {
        this.isLoading = true
        await this.onCreate()
        this.isLoading = false
        if (this.operation === 'copy') {
          this.$store.commit('document/DOCUMENT_SET_OPERATION', '')
          this.$store.commit('document/DOCUMENT_SET_NAV_TYPE', 'unpublished')
          wx.redirectTo({
            url: '/pages/documents/main'
          })
          return
        }
        wx.navigateBack({
          delta: 3
        })
        this.$store.commit('document/DOCUMENT_UNSHIRFT_DOCUMENTS', this.document)
        this.$store.commit('document/DOCUMENT_SET_NAV_TYPE', 'unpublished')
        this.$store.commit('UI_SET_TOAST_SUCCESS_MSG', '已保存至草稿箱')
      } catch (e) {
        this.$debug({err: e})
      }
    },
    onPublish () {
      wx.redirectTo({
        url: '/pages/document-publish/main'
      })
    }
  }
}
</script>

<style scoped>
</style>
